package hot100;


public class Lc74 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int[] col1 = new int[matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            col1[i] = matrix[i][0];
        }
        int line = binary_search_ge(col1, target);
        System.out.println(line);
        if (line == -100) {
            return true;
        }
        if (line < 0) {
            return false;
        }
        int ret = binary_search_ge(matrix[line], target);
        return ret == -100;
    }

    private int binary_search_ge(int[] nums, int target) {
        int left = -1;
        int right = nums.length;
        while (left + 1 < right) {
            int mid = (right - left) / 2 + left;
            if (nums[mid] < target) {
                left = mid;
            }
            else if (nums[mid] > target) {
                right = mid;
            }
            else {
                return -100;
            }
        }
        return left;
    }
}
